package fr.mywiki.business.file;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ejb.ObjectNotFoundException;

import fr.mywiki.model.ejbAdapter.ServicesFacade;
import fr.mywiki.model.ejbAdapter.utils.PersistenceException;
import fr.mywiki.model.queries.DatabaseFileController;

/** Contains several methods for the manipulation of files. */
public class FileManager {
	/** Extensions of image files */
	public static final String[] IMAGE_EXTENSIONS = { "jpg", "jpeg", "gif", "png" };
	
	/** Extensions of recognized non-image files */
	public static final String[] DOCUMENT_EXTENSIONS = { "tgz", "gz", "zip",
			"pdf", "txt", "html", "htm", "ps", "doc", "xls", "ppt", "rtf", "xml",
			"rpm", "deb", "conf", "sxw", "sxc", "sxi", "sxd", "odc", "odf", "odg",
			"odi", "odp", "ods", "odt" };
	
	/**
	 * Returns the extension of the file given in parameter.
	 * 
	 * @param file a <code>FileLite</code> object
	 * @return the <code>String</code> extension of the file, eventually an
	 *         empty String (if the name of the file did not have an extension)
	 */
	public static String getFileExtension(FileLite file) {
		return getFileExtension(file.getName());
	}
	
	/**
	 * Returns the extension of the file name given in parameter.
	 * 
	 * @param fileName the name of the file to test
	 * @return the <code>String</code> extension of the file, eventually an
	 *         empty String (if the name of the file did not have an extension)
	 */
	public static String getFileExtension(String fileName) {
		if (fileName != null) {
			String ext = fileName.substring(fileName.lastIndexOf('.') + 1);
			if (ext.equals("jpg"))
				ext = "jpeg";
			return ext;
		}
		return new String();
	}
	
	/**
	 * Specifies if the file given in parameter is an image file or not.
	 * 
	 * @param file
	 *          a <code>FileLite</code> object
	 * @return true if the extension of the file name belongs to the list of
	 *         recognized image extensions.
	 */
	public static boolean isImageFile(FileLite file) {
		return Arrays.asList(IMAGE_EXTENSIONS).contains(getFileExtension(file));
	}
	
	/**
	 * Specifies if the file name given in parameter corresponds to an image file
	 * or not.
	 * 
	 * @param fileName
	 *          the name of the file to test
	 * @return true if the extension of the name belongs to the list of recognized
	 *         extensions.
	 */
	public static boolean isImageFile(String fileName) {
		return Arrays.asList(IMAGE_EXTENSIONS).contains(getFileExtension(fileName));
	}
	
	/**
	 * Uploads the specified file into the database with the given name.
	 * 
	 * @param name the name of the file.
	 * @param data the content of the file.
	 * @return a <code>FileLite</code> object representing the created database object.
	 */
	public static FileLite uploadFile(String name, byte[] data)
			throws ObjectNotFoundException, RemoteException,
			PersistenceException {
		FileLite file = DatabaseFileController.createFile();
		
		file.setName(name);
		file.setContent(data);
		file.update();
		
		return file;
	}
	
	/**
	 * Returns all the file descriptors for all the files of the database.
	 * 
	 * @return a <code>List</code> of <code>FileLite</code> objects,
	 *         themselves being in light mode.
	 * TODO test it.
	 */
	public static List getAllFilesLight() throws RemoteException {
		return new ArrayList(ServicesFacade.getInstance().findAll(
				FileLite.TYPE_ENT_LIGHT));
	}
	
	/**
	 * Returns all the file descriptors for image files of the database, i.e. the
	 * files whose name ends with a image extension.
	 * 
	 * @return a <code>List</code> of <code>FilterLite</code> objects in light mode.
	 * TODO Test it.
	 */
	public static List getAllImagesFilesLight() throws RemoteException {
		return new ArrayList(DatabaseFileController.findAllImageFileLight());
	}
	
	/**
	 * Returns the file whose name is exactly the one given in parameter.
	 * 
	 * @param name the desired name
	 * @return a light <code>FileLite</code> object or <code>null</code>
	 */
	public static FileLite findFileByName(String name) throws RemoteException {
		return DatabaseFileController.findFileByName(name, FileLite.TYPE_ENT_LIGHT);
	}
}
